package com.glela.api.pay.yeepay;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.glela.api.pay.yeepay.model.YeePayRequest;
import com.glela.common.util.HttpUtil;
import com.glela.common.util.YeePayAESUtil;
import com.glela.common.util.YeePayDigest;

/**
 * 
 * @author MaoZeYao
 *
 */
public class QRDemo {
	
	public static final String SECRET_KEY = "lF0386j067MWZb7817003M8o32iW7918H9Fk3JRV758P10lp182T7B8zte2u";
	public static final String CUSTOMER_NUMBER = "10013842859";
	
	//public static final String SECRET_KEY = "png6085B44o8006YDH6gM842Rd0157005Y2M5MDgVAS8D06758v23tqmZS37";
	//public static final String CUSTOMER_NUMBER = "10000449591";
	
	// public static final String CUSTOMER_NUMBER = "10013731542";
	// public static final String SECRET_KEY = "6jswi9D654im6ZuNYXc73567ld2S4vg8590KAZ3Zp5do7K19353vj25395l9";
	
	public static void main(String[] args) throws Exception {
		/*String dataJson = "{\"data\":\"89504E470D0A1A0A0000000D494844520000008B0000008B0802000000DE7BFDA8000003B14944415478DAEDDCC172A330144451FFFF4F3BCB2C520EF0FAB604E5CBD28309D25159523FD5BCDE5EF7BE5E76C133845ED9F5FBB8CFFFF4DF4B8CFED6DFAFFFF3C9B54E39D1C04BAF91F4AA420A29C40ACD5A3213C25B1BBE183E1A905E554821854A42B38E08671DAA8B67A38A7AE06C9C29A490428F100A39A961B1604D3C6BB2420A29F474216A7FDEDEA85F5D64F766538514526897D02C53E8CD16616C71A9FB70C51BA53E0A29F4F542D4D2D34FEE5EC1F31385BE4C083B98D2AF558725A85ECF76CFFA28A4904299D02C10A4EEA1D2552A4AA04A626180AB90420A814254AFE1F7F48EF8E0238652544821851A4278ED67D6C5E1CA7E764268969CCE9A03A43E0A29A41024841789A9B250383E665F0FA7BAD936402185145A23D4CB1CC349AB57A6C2AB65B3CC452185146A0851D58E701E0A4F11E12F8627C2F36C5B21851482EA4354ACD9FE99CE8757B8ECA6866077A5A090420A65392955D709BB385CF4530325CC64155248A18610C5804F0054393C0C55C39B933295420A29840885ADA556C954CD262C752F780E999C2AA4904257D67261FD380C0E4286B060DF3B72942CBB155248214408DF7B53CBDC05E76F661324B5F738BC592185146A0BE1EF3DAB452DDBC3BF37FDCFFB97330585145268949C523D3BEBE2766BABEB662A4A504821851A422B13845E26BBA58C3DDB06009982420A2904E572D45B86530BFEBBDF6B0E9BA728A490428810B5550EC385DED91A9C73D90E412185146285F02207BE225F302CB61C15FAF475851452A82D142E8EA93C7141BE396B0E35E00EAAE00A29A4102D14A6872B6FC6F7030B80CFBF98420A2984086DA9798705A730CD0C57ED78090A388DA590420A9D109A2D22F1C5FAB2F6E7F3622F6B56482185AA4254FCB7202FC0C350AA37908D8A420A29C40A852F87B73614C25F950A942F27A70A29A410547D9895B1D9D32D0DB3DE3C44A5AB079982420A29440B6D4921F17E9CFD09AA5E351BEE67E7218514522813429EFE8E0FE2E015EEDE42BC34AA1452482156285C37CFE6B39EFDCAE33BD47478300F29A4904223A15E19865A5B53894638D551A1EAF9272BA49042881075E1BFFB78AF5163884A220EDBA590420A2142F8D6BDF7EB4CADDAC3DA796F1771F69C82420A293412A2661DAAFD540AB9853C2CA429A4904255A159AF850B68EA1EBC86545A499F69A9420A29742BA170B2096FC613D82DAD504821851E21B4A07A8D9F19A2E657F6F090420A295412C26BD50BECC3724E6F5E4C4E0028A49042AC105E1FA23A6B36375065ECA48B433C8514520811F2BAEDA5D0DDAF1F1E02B8A670D841600000000049454E44AE426082\"}";
		dataJson = YeePayAESUtil.decrypt(JSONObject.parseObject(dataJson).get("data").toString(), SECRET_KEY.substring(0, 16));
		System.out.println(dataJson);*/
		doPostMethod ();
		 
	}
	
	public static void doPostMethod () throws IOException {
		YeePayRequest model = new YeePayRequest();
		
		String data = getYeePayWeChatString(model);
		String content = "customernumber=" + CUSTOMER_NUMBER + "&data=" + data;
		String url = "https://o2o.yeepay.com/zgt-api/api/pay";
		String result = HttpUtil.doPost(content, url, "application/x-www-form-urlencoded");
		System.out.println(result);
		result = YeePayAESUtil.decrypt(JSONObject.parseObject(result).get("data").toString(), SECRET_KEY.substring(0, 16));
		System.out.println("解密：" + result);
	}
	
	public static String getYeePayWeChatString (YeePayRequest model) {
		String data = "";
		try {
			String customernumber = CUSTOMER_NUMBER; // 主账号商户编号
			String requestid = model.getRequestid(); // 订单号必须在该商编下唯一，由商户输入
			String amount = model.getAmount(); // 单位：元.必须大于等于0.01
			String assure = model.getAssure(); // 1 – 担保交易 0 – 非担保交易
			String productname = model.getProductname();// 商品名称
			String productcat = model.getProductcat(); // 商品类别
			String productdesc = model.getProductdesc(); // 商品描述
			String divideinfo = model.getDivideinfo(); // "10013422494:0.0015|10013422911:0.0125"
			String callbackurl = model.getCallbackurl(); // 服务器点对点通知地址
			String webcallbackurl = model.getWebcallbackurl();
			String bankid = model.getBankid(); // 该参数仅当支付产品类别为快捷,即参数payproducttype=SALES时 才有效
			String period = model.getPeriod(); //
			String memo = model.getMemo(); // 商户备注
			String hmac = ""; // 签名信息
			String keyForAes = SECRET_KEY.substring(0, 16);
			String[] stringArray = { customernumber, requestid, amount, assure, productname, productcat, productdesc,
					divideinfo, callbackurl, webcallbackurl, bankid, period, memo };
			// 签名完成
			hmac = YeePayDigest.getHmac(stringArray, SECRET_KEY);
			
			// 订单的有效时间 ！！！！！！！！！！！！！！！！！！！！！！！
			String orderexpdate = model.getOrderexpdate();
			
			// 以下参数为快捷支付时才需要传递
			String payproducttype = "ONEKEY"; // 快捷固定值：SALES；当该参数 的值为SALES时，表示支付产品 类型为快捷支付。
			String appid = "wx65d97c0cfa18b4f8";
			String userno = "o7lhVs7RKUSoBEgsbsZaiKLDjuMA"; // 商户用来表示用户的唯一标识
			//String userno = "o7nB3syGBdBYv3GnEk1u6iaP-a7k"; // 商户用来表示用户的唯一标识
			//String appid = "wx204752eed53acd80";
			
			//String payproducttype = "ONEKEY"; // 快捷固定值：SALES；当该参数 的值为SALES时，表示支付产品 类型为快捷支付。
			//String userno = "152"; // 商户用来表示用户的唯一标识
			
			Map<String, String> dataMap = new HashMap<String, String>();
			dataMap.put("customernumber", customernumber);
			dataMap.put("requestid", requestid);
			dataMap.put("amount", amount);
			dataMap.put("assure", assure);
			dataMap.put("productname", productname);
			dataMap.put("productcat", productcat);
			dataMap.put("productdesc", productdesc);
			dataMap.put("divideinfo", divideinfo);
			dataMap.put("callbackurl", callbackurl);
			dataMap.put("webcallbackurl", webcallbackurl);
			dataMap.put("bankid", bankid);
			dataMap.put("period", period);
			dataMap.put("memo", memo);
			
			dataMap.put("orderexpdate", orderexpdate);
			
			// 微信参数
			dataMap.put("payproducttype", payproducttype);
			dataMap.put("openid", userno);
			dataMap.put("appid", appid);
				
			dataMap.put("ip", model.getUserIp());
			dataMap.put("directcode", "WAP_WECHATG");
			
			dataMap.put("hmac", hmac);
			
			String dataJsonString = JSONObject.toJSONString(dataMap); // 生成data
			System.out.println(dataJsonString);
			// AES加密
			data = YeePayAESUtil.encrypt(dataJsonString, keyForAes);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return data;
	}
	
	/*protected String getYeePayNetSilverString (YeePayRequest model) {
		String data = "";
		try {
			String customernumber = YeePay.CUSTOMER_NUMBER; // 主账号商户编号
			String requestid = model.getRequestid(); // 订单号必须在该商编下唯一，由商户输入
			String amount = model.getAmount(); // 单位：元.必须大于等于0.01
			String assure = model.getAssure(); // 1 – 担保交易 0 – 非担保交易
			String productname = model.getProductname();// 商品名称
			String productcat = model.getProductcat(); // 商品类别
			String productdesc = model.getProductdesc(); // 商品描述
			String divideinfo = model.getDivideinfo(); // "10013422494:0.0015|10013422911:0.0125"
			String callbackurl = model.getCallbackurl(); // 服务器点对点通知地址
			String webcallbackurl = model.getWebcallbackurl();
			String bankid = model.getBankid(); // 该参数仅当支付产品类别为快捷,即参数payproducttype=SALES时 才有效
			String period = model.getPeriod(); //
			String memo = model.getMemo(); // 商户备注
			String hmac = ""; // 签名信息
			String keyForAes = YeePay.SECRET_KEY.substring(0, 16);
			String[] stringArray = { customernumber, requestid, amount, assure, productname, productcat, productdesc,
					divideinfo, callbackurl, webcallbackurl, bankid, period, memo };
			// 签名完成
			hmac = YeePayDigest.getHmac(stringArray, YeePay.SECRET_KEY);
			
			// 订单的有效时间 ！！！！！！！！！！！！！！！！！！！！！！！
			String orderexpdate = model.getOrderexpdate();
			
			// 以下参数为快捷支付时才需要传递
			String payproducttype = model.getPayproducttype(); // 快捷固定值：SALES；当该参数 的值为SALES时，表示支付产品 类型为快捷支付。
			String userno = model.getUserno(); // 商户用来表示用户的唯一标识
			String cardname = model.getCardname(); // 持卡人姓名，如传送过来则会限 制用户只能用对应这个开户名的 银行卡支付
			String idcard = model.getIdcard(); // 身份证号
			String bankcardnum = model.getBankcardnum(); // 银行卡号
			String mobilephone = model.getMobilephone(); // 银行预留手机号
			
			Map<String, String> dataMap = new HashMap<String, String>();
			dataMap.put("customernumber", customernumber);
			dataMap.put("requestid", requestid);
			dataMap.put("amount", amount);
			dataMap.put("assure", assure);
			dataMap.put("productname", productname);
			dataMap.put("productcat", productcat);
			dataMap.put("productdesc", productdesc);
			dataMap.put("divideinfo", divideinfo);
			dataMap.put("callbackurl", callbackurl);
			dataMap.put("webcallbackurl", webcallbackurl);
			dataMap.put("bankid", bankid);
			dataMap.put("period", period);
			dataMap.put("memo", memo);
			
			dataMap.put("orderexpdate", orderexpdate);
			
			// 快捷参数
			dataMap.put("payproducttype", payproducttype);
			dataMap.put("userno", userno);
			dataMap.put("cardname", cardname);
			dataMap.put("idcard", idcard);
			dataMap.put("bankcardnum", bankcardnum);
			dataMap.put("mobilephone", mobilephone);
			
			dataMap.put("ip", model.getIp());
			
			dataMap.put("hmac", hmac);
			
			String dataJsonString = JSON.toJSONString(dataMap); // 生成data
			// AES加密
			data = YeePayAESUtil.encrypt(dataJsonString, keyForAes);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return data;
	}*/
	
}
